package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.Utils;

/**
 * Created by yangchuan on 2016/5/20.
 */
public class ResetpasswordActivity extends BaseAppCompatActivity implements RequestListener
{
    private EditText historypwd;
    private EditText newpwd;
    private String   phone, vcode;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword);
        Utils.initTitle("找回密码", ResetpasswordActivity.this, true);
        if (getIntent() != null)
        {
            phone = getIntent().getStringExtra("cellphone");
            vcode = getIntent().getStringExtra("vcode");
        }
        init();
//        phone.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//密码可见
//        phone.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码隐藏
    }

    private void init()
    {
        historypwd = (EditText) findViewById(R.id.reset_pwd_history);
        newpwd = (EditText) findViewById(R.id.reset_pwd_new);
        findViewById(R.id.reset_next_btn).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String pwdhis = historypwd.getText().toString().trim();
                String pwdnew = newpwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwdhis))
                {
                    Toast.makeText(ResetpasswordActivity.this, "请输入新密码！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pwdnew))
                {
                    Toast.makeText(ResetpasswordActivity.this, "请确认新密码！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!pwdhis.equals(pwdnew))
                {
                    Toast.makeText(ResetpasswordActivity.this, "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }
                RequestMethod.getMethod().appUpdatePassword(phone, vcode, pwdhis, ResetpasswordActivity.this, ResetpasswordActivity.this);
            }
        });
    }

    @Override
    public void requestSuccess(String tag, String message)
    {
        if (tag.equals(RequestTag.APPUPDATEPASSWORD))
        {

            Intent intent = new Intent(ResetpasswordActivity.this, ResetSuccessActivity.class);

            intent.putExtra("result", message);
            startActivity(intent);

        }
    }

    @Override
    public void requestError(String message)
    {
        FailureResultObject result = JsonParser.getJsonParser().parserJsonToObj(message, FailureResultObject.class);
        Log.e("error", result.toString());
        Toast.makeText(ResetpasswordActivity.this, result.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }

}
 
